[์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step - ๋ฐ์ฌ์ฑ]์ ์ฝ๊ณ ์ธ์ฉํ๊ณ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
"ํ ์คํธ์ ๋ฆฌํฉํ ๋ง์ ๊ฐ๋ฐ์๊ฐ ๊ฐ์ถ์ด์ผ ํ ์ค์ํ ์ญ๋์ด๋ค."
๐ฉ main() ๋ฉ์๋๋ฅผ ํ์ฉํ ํ ์คํธ์ ๋ฌธ์ ์
์ผ๋ฐ์ ์ธ ํ
์คํธ ๋ฐฉ๋ฒ์ main() ๋ฉ์๋๋ฅผ ํ์ฉํด ์ฐ๋ฆฌ๊ฐ ์๋ํ ๊ฒฐ๊ณผ ๊ฐ์ด ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋๋์ง๋ฅผ ์ฝ์์ ํตํด ํ์ธํ๋ ๊ฒ์ด๋ค.
public class Calculator {
int add (int x, int y) {
return x + y;
}
int subtract (int x, int y) {
return x - y;
}
int multiply (int x, int y) {
return x * y;
}
int divide (int x, int y) {
return x / y;
}
public static void main(String[] args) {
Calculator cal = new Calculator();
System.out.println(cal.add(3,4));
System.out.println(cal.subtract(5,4));
System.out.println(cal.multiply(2,6));
System.out.println(cal.divide(8,4));
}
}
๐ง ๊ณ์ฐ๊ธฐ ์ฝ๋
-
์ค์ ์๋น์ค๋ฅผ ๋ด๋นํ๋ ํ๋ก๋์ ์ฝ๋(production code)์ ์ด ํ๋ก๋์ ์ฝ๋๊ฐ ์ ์ ๋์ํ๋์ง ํ์ธํ๋
main()์ผ๋ก ๋๋๋ค.- ์ผ๋ฐ์ ์ผ๋ก
main()์ ํ๋ก๊ทธ๋๋ฐ์ ์คํํ๊ธฐ ์ํ ๋ชฉ์ ๊ณผ ํ๋ก๋์ ์ฝ๋๊ฐ ์ ์ ๋์ํ๋์ง ํ์ธํ๊ธฐ ์ํ ๋ชฉ์ ์ผ๋ก ๋๋๋ค.
- ์ผ๋ฐ์ ์ผ๋ก
๐งญ ์ฝ๋์ ๋ฌธ์ ์ !
-
ํ๋ก๋์ ์ฝ๋์ ํ ์คํธ ์ฝ๋(
main()๋ฉ์๋)๊ฐ ๊ฐ์ ํด๋์ค์ ์์นํ๊ณ ์๋ค.- ํ ์คํธ ์ฝ๋์ ๊ฒฝ์ฐ ํ ์คํธ ๋จ๊ณ์์๋ง ํ์ํ๋ฏ๋ก ๊ตณ์ด ์๋น์คํ๋ ์์ ์ ๊ฐ์ด ๋ฐฐํฌํ ํ์๊ฐ ์๋ค.
- ๋ฐ๋ผ์ ๋ฌธ์ ํด๊ฒฐ์ ์ํด ํ๋ก๋์
์ฝ๋(
Calculatorํด๋์ค)์ ํ ์คํธ ์ฝ๋(CalculatorTest)๋ฅผ ๋ถ๋ฆฌํ๋ค.
public class CalcultorTest {
public static void main(String[] args) {
Calculator cal = new Calculator();
System.out.println(cal.add(3,4));
System.out.println(cal.subtract(5,4));
System.out.println(cal.multiply(2,6));
System.out.println(cal.divide(8,4));
}
}
-
ํ ์คํธ๋ฅผ ๋ด๋นํ๋ ๋ณ๋์ ํด๋์ค๋ฅผ ์ถ๊ฐํ์ง๋ง
main()๋ฉ์๋ ํ๋์ ํ๋ก๋์ ์ฝ๋์ ์ฌ๋ฌ ๋ฉ์๋๋ฅผ ๋์์ ํ ์คํธํ๊ณ ์๋ค.- ํ๋ก๋์
์ฝ๋์ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ ์๋ก,
main()๋ฉ์๋์ ๋ณต์ก๋๋ ์ฆ๊ฐํ๊ฒ ๋๊ณ , ์ด๋ ๊ฒฐ๊ณผ์ ์ผ๋กmain()๋ฉ์๋ ์ ์ง์ ๋ถ๋ด์ด ๋๋ค.
- ํ๋ก๋์
์ฝ๋์ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ ์๋ก,
ํจ์ ์ ์ง ๋ณด์๋ฅผ ์ํด ํ ์คํธ ์ฝ๋๋ฅผ ๊ฐ ๋ฉ์๋๋ณ ๋ถ๋ฆฌ
public class CalcultorTest {
public static void main(String[] args) {
// main์์๋ ํจ์ ํธ์ถ๋ง ์ํ
Calculator cal = new Calculator();
add(cal);
subtract(cal);
multiply(cal);
divide(cal);
}
private static void divide(Calculator cal) {
System.out.println(cal.divide(8,4));
}
private static void multiply(Calculator cal) {
System.out.println(cal.multiply(2,6));
}
private static void subtract(Calculator cal) {
System.out.println(cal.subtract(5,4));
}
private static void add(Calculator cal) {
System.out.println(cal.add(3,4));
}
}
-
์ด ๋ํ ๊ฐ๋ฐ์๊ฐ ํ๋ก๊ทธ๋๋ฐํ๋ ๊ณผ์ ์ ์ดํด๋ณด๋ฉด, 2๊ฐ์ง ์ด์ ์์ ๊ฒฐ๊ตญ ์ต์ข ์ ์ธ ํด๊ฒฐ์ฑ ์ด ๋ ์ ์์์ ์ ์ ์๋ค.
- ํด๋์ค๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ชจ๋ ๋ฉ์๋์ ๊ด์ฌ์ด ์๋ ๊ฒ์ด ์๋๋ผ ํ์ฌ ๋ด๊ฐ ๊ตฌํํ๋ ๋ฉ์๋์๋ง ์ง์คํ๊ณ ์ถ๋ค.
- ํ์ง๋ง ์ ํ
์คํธ ์ฝ๋๋
Calculatorํด๋์ค์ ๋ชจ๋ ๋ฉ์๋๋ฅผ ํ ์คํธํ ์ ๋ฐ์ ์๋ค. - ๊ทธ๋ ๋ค๊ณ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ์ฃผ์ ์ฒ๋ฆฌํ๋ ๊ฒ ๋ํ ๋ถํฉ๋ฆฌ์ ์ด๋ค.
-
๋ ๋ค๋ฅธ ๋ฌธ์ ์ ์, ํ ์คํธ ๊ฒฐ๊ณผ๋ฅผ ๋งค๋ฒ ์ฝ์์ ์ถ๋ ฅ๋๋ ๊ฐ์ ํตํด ์๋์ผ๋ก ํ์ธํด์ผ ํ๋ค๋ ๊ฒ์ด๋ค.
- ๋ก์ง์ด ๊ฐ๋จํ ๊ฒฝ์ฐ์๋ ๊ฒฐ๊ณผ ๊ฐ์ ์ฝ๊ฒ ์์ธกํ ์ ์๋ค.
- ํ์ง๋ง ๋ก์ง์ ๋ณต์ก๋๊ฐ ๋์ ๊ฒฝ์ฐ, ํ์ ๊ฒฐ๊ณผ ๊ฐ์ ์์ธกํ๋ ๊ฒ์ ๋งค์ฐ ๋ฒ๊ฑฐ๋กญ๊ณ , ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ์์ ์ผ ๊ฒ์ด๋ค.
main() ๋ฉ์๋๋ฅผ ํ์ฉํ ํ
์คํธ์ ์ด ๊ฐ์ ๋ฌธ์ ์ ์ ํด๊ฒฐํ๊ธฐ ์ํด ๋ฑ์ฅํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ JUnit ์ด๋ค.
- ์ด๋ ๋ด๊ฐ ๊ด์ฌ ๊ฐ์ง๋ ๋ฉ์๋์ ๋ํ ํ ์คํธ๋ง ๊ฐ๋ฅํ๊ณ ๋ก์ง ์คํ ํ์ ๊ฒฐ๊ณผ ๊ฐ ํ์ธ์ ํ๋ก๊ทธ๋๋ฐ์ ํตํด ์๋ํํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
๐ง JUnit์ ํ์ฉํด main() ๋ฉ์๋ ๋ฌธ์ ์ ๊ทน๋ณต
JUnit์ ๋จ์ ํ ์คํธ ํ๋ ์์ํฌ ์ค ํ๋์ด๋ค. ์ฌ์ฉํ๊ธฐ ์ฝ๊ณ ํ์ต ๋น์ฉ์ด ๋ฎ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๐งญ ์ฒซ ๋ฒ์งธ ๋ฌธ์ ํด๊ฒฐ, ํ ๋ฒ์ ๋ฉ์๋ ํ๋์๋ง ์ง์ค
import org.junit.Test;
public class CalculatorTest {
@Test
public void add() {
Calculator cal = new Calculator();
System.out.println(cal.add(6,3));
}
}
- JUnit์ ํ
์คํธ ๋ฉ์๋์
@Testannotation์ ์ถ๊ฐํ๋ค. (ํ ์คํธ ๋ฉ์๋๋ก ์ธ์ํ๊ธฐ ์ํจ!) - ์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๋ค๋ฅธ ๋ฉ์๋ ์ฝ๋๋ ์์ฑํ๋ค.
-
JUnit ๊ธฐ๋ฐ์ผ๋ก ํ ์คํธ ์ฝ๋๋ฅผ ๊ตฌํํ๋ฉด
CalculatorTestํด๋์ค๊ฐ ๊ฐ์ง๋ ์ ์ฒด ๋ฉ์๋๋ฅผ ํ ๋ฒ์ ์คํํ ์๋ ์์ผ๋ฉฐ, ๋ฉ์๋ ๊ฐ๊ฐ์ ์คํํ ์๋ ์๋ค.- ๋ค๋ฅธ ๋ฉ์๋์ ์ํฅ์ ๋ฐ์ง ์๊ณ ๊ตฌํ ์ค์ธ ํ๋ก๋์ ์ฝ๋์ ์ง์คํ ์ ์๋ค.
"์ค์ํ ๊ฒ์, ํ ์คํธ ์ฝ๋๋ ์์ฑํ ์์๋๋ก ์คํ๋์ง ์๋๋ค!"
๐งญ ๋ ๋ฒ์งธ ๋ฌธ์ ํด๊ฒฐ, ๊ฒฐ๊ณผ ๊ฐ์ ๋์ด ์๋ ํ๋ก๊ทธ๋จ์ ํตํด ์๋ํ
main()๋ฉ์๋์ ๋ ๋ฒ์งธ ๋ฌธ์ ์ ์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋์ผ๋ก ์ง์ ํ์ธํด์ผ ํ๋ค๋ ๊ฒ์ด๋ค. JUnit์ ์ด ๋ฌธ์ ์ ๊ทน๋ณต์ ์ํดassertEquals()๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void add() {
Calculator cal = new Calculator();
assertEquals(9, cal.add(6,3));
}
@Test
public void subtract() {
Calculator cal = new Calculator();
assertEquals(3, cal.subtract(6,3));
}
}
assertEquals๋ static ๋ฉ์๋์ด๋ฏ๋กimport static์ผ๋ก ๋ฉ์๋๋ฅผ importํ์ฌ ์ฌ์ฉํด์ผ ํ๋ค.- ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ ๊ธฐ๋ํ๋ ๊ฒฐ๊ณผ๊ฐ(expected) ์ด๊ณ , ๋ ๋ฒ์งธ ์ธ์๋ ํ๋ก๋์ ์ฝ๋์ ๋ฉ์๋๋ฅผ ์คํํ ๊ฒฐ๊ณผ๊ฐ(actual) ์ด๋ค.
int,long,String๋ฑ ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ์ง์ํ๋ค.- ํ ์คํธ๊ฐ ์คํจํ ๊ฒฝ์ฐ๋ ์๋ ์ํ์ฐฝ์์ ์คํจ ์์ธ์ ํ์ธํ ์ ์๋ค.
JUnit์ assertEquals() ๋ฉ์๋๋ฅผ ํ์ฉํ๋ฉด ์คํ ๊ฒฐ๊ณผ๋ฅผ ์๋ํํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค. JUnit์ Assert ํด๋์ค๋ ์ด์ธ์๋ ๋ค์๊ณผ ๊ฐ์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
- ๊ฒฐ๊ณผ ๊ฐ์ T/F ์ ๋ฌด๋ฅผ ํ์ธํ๋
assertTrue(),assertFalse() - ๊ฒฐ๊ณผ ๊ฐ์ null ์ ๋ฌด๋ฅผ ํ๋จํ๋
assertNull(),assertNotNull() - ๋ฐฐ์ด ๊ฐ์ด ๊ฐ์์ง ๊ฒ์ฆํ๋
assertArrayEquals()
๐งญ ํ ์คํธ ์ฝ๋ ์ค๋ณต ์ ๊ฑฐ
"๊ฐ๋ฐ์๊ฐ ๊ฐ์ ธ์ผ ํ ์ข์ ์ต๊ด ์ค ํ๋๋ ์ค๋ณต ์ฝ๋๋ฅผ ์ ๊ฑฐํ๋ ๊ฒ์ด๋ค."
์์ ๊ตฌํํ CalculatorTest ํด๋์ค์์, Calculator ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๋ถ๋ถ์์ ์ค๋ณต์ด ๋ฐ์ํ๋ค. ์ด๋ฌํ ์ค๋ณต์ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ฑฐํ ์ ์๋ค.
public class CalculatorTest {
private Calculator cal = new Calculator();
@Test
public void add() {
assertEquals(9, cal.add(6,3));
}
@Test
public void subtract() {
assertEquals(3, cal.subtract(6,3));
}
}
- ์ด๋ฌํ ๋ฐฉ์์ ์๋ฐ ๋ฌธ๋ฒ์ ์์ด ์๋ฌด๋ฐ ๋ฌธ์ ๊ฐ ๋์ง ์์ง๋ง, JUnit์ ํ
์คํธ๋ฅผ ์คํํ๊ธฐ ์ํ ์ด๊ธฐํ ์์
์ ์ด๋ ๊ฒ ํ์ง ์๊ณ
@Beforeannotation์ ํ์ฉํ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
private Calculator cal;
@Before
public void setup() {
cal = new Calculator();
}
@Test
public void add() {
assertEquals(9, cal.add(6,3));
}
@Test
public void subtract() {
assertEquals(3, cal.subtract(6,3));
}
}
-
์ด์ ๊ฐ์ด
@Beforeannotation์ ํ์ฉํ์ฌ๋ ๊ฐ ๋จ์ ํ ์คํธ๋ฅผ ์คํํ ๋๋ง๋คCalculator์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ฒ์ ๊ฐ๋ค.- ์ด๋ ๊ฒ ๋งค ํ
์คํธ๋ง๋ค
Calculator์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์ด์ ๋ ํน์ ํ ์คํธ ๋ฉ์๋๋ฅผ ์คํํ ๋,Calculator์ ์ํ ๊ฐ์ด ๋ณ๊ฒฝ๋์ด ๋ค์ ๋ฉ์๋์ ์ํฅ์ ๋ฏธ์น ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. - ๋ค์ ํ
์คํธ ๋ฉ์๋์ ์ํฅ์ ๋ฏธ์น๊ฒ ๋๋ฉด, ํ
์คํธ ์คํ ์์๋
Calculator์ ์ํ ๊ฐ์ ๋ฐ๋ผ ํ ์คํธ์ ์ฑ๊ณต ์ฌ๋ถ๊ฐ ๋ฌ๋ผ์ง ์ ์๋ค.
- ์ด๋ ๊ฒ ๋งค ํ
์คํธ๋ง๋ค
๊ทธ๋ผ์๋ ์ฝ๋ฉ๋์ด ๋ ๋ง์ ๊ตฌํ ๋น์ฉ์ด ํฐ @Before annotation์ ์ฌ์ฉํ๋ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
- JUnit์๋
@RunWith,@Rule๊ฐ์ annotation์ ์ฌ์ฉํด ๊ธฐ๋ฅ์ ํ์ฅํ ์ ์๋๋ฐ,@Before์์ด์ด์ผ๋ง ํด๋น annotation์์ ์ด๊ธฐํ๋ ๊ฐ์ฒด ์ ๊ทผํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. - ๋ฐ๋ผ์, ๊ฐ๋ฅํ
@Beforeannotation์ ์ฌ์ฉํด ํ ์คํธ ๋ฉ์๋์ ๋ํ ์ด๊ธฐํ ์์ ์ ํ๋ ๊ฒ์ด ๋ฌธ์ ๋ฐ์์ ๊ฐ๋ฅ์ฑ์ ๋ฎ์ถฐ์ค๋ค.
@Before annotation๊ณผ ๋น์ทํ๊ฒ, @After annotation์ผ๋ก ๋ฉ์๋ ์คํ์ด ๋๋ ํ ํ์ฒ๋ฆฌ ์์
์ ๋ด๋นํ๋ annotation๋ ์กด์ฌํ๋ค.
๐ง ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ์๊ตฌ์ฌํญ ๋ฐ ์ค์ต
์๊ตฌ์ฌํญ: ์ ๋ฌํ๋ ๋ฌธ์๋ฅผ ๊ตฌ๋ถ์๋ก ๋ถ๋ฆฌํ ํ ๊ฐ ์ซ์์ ํฉ์ ๊ตฌํด ๋ฐํํด์ผ ํ๋ค.
- ์ผํ ๋๋ ์ฝ๋ก ์ ๊ตฌ๋ถ์๋ก ๊ฐ์ง๋ ๋ฌธ์์ด์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ ๊ฐ ์ซ์์ ํฉ์ ๋ฐํ
- ์์ ๊ธฐ๋ณธ ๊ตฌ๋ถ์ ์ธ์ Custom ๊ตฌ๋ถ์๋ฅผ ์ง์ ํ ์ ์๋ค. Custom ๊ตฌ๋ถ์๋ ๋ฌธ์์ด ์๋ถ๋ถ์ "//"์ "\n" ์ฌ์ด์ ์์นํ๋ ๋ฌธ์๋ก ์ฌ์ฉ
- ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ์ ์์๋ฅผ ์ ๋ฌํ๋ ๊ฒฝ์ฐ
RuntimeException์ผ๋ก ์์ธ ์ฒ๋ฆฌ
๊ฐ๋จํด๋ณด์ด์ง๋ง ๊ตฌํ์ ๋ฐ๋ก ์์ํ์ง ์๊ณ , ์๊ตฌ์ฌํญ์ ๋ ์์ ๋จ์๋ก ๋๋ ํ ์คํธํ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ถ๋ฆฌํด๋ณธ๋ค.
๐งญ ์๊ตฌ์ฌํญ ๋ถ๋ฆฌ ๋ฐ ๊ฐ ๋จ๊ณ๋ณ ํํธ
- ์ต๋ํ ํํธ ์์ด ์งํ !
๐งญ ์ถ๊ฐ ์๊ตฌ์ฌํญ
- ์๊ตฌ์ฌํญ์ ๋ง์กฑํ๋ ์ฝ๋๋ฅผ ๊ตฌํํ๋ค๊ณ ํด์ ๊ฐ๋ฐ์ด ์๋ฃ๋ ๊ฒ์ ์๋๋ค. ๊ตฌํ ์๋ฃ ์ดํ, ์ค๋ณต ์ ๊ฑฐ & ์ฝ๊ธฐ ์ข์ ์ฝ๋ ๊ตฌํ์ ์ํ ๋ฆฌํฉํ ๋ง(๊ตฌ์กฐ ๋ณ๊ฒฝ)์ด ์ด๋ค์ ธ์ผ ํ๋ค.
๋ฆฌํฉํ ๋ง : ์์ค์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ ์ ์ง๋ณด์๋ฅผ ํธํ๊ฒ ํ๊ธฐ ์ํด ์์ค์ฝ๋์ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ๋ฆฌํฉํ ๋ง์ ํ๋๋ผ๋ ๊ธฐ๋ฅ์์ ๊ฒฐ๊ณผ๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒ์ ์๋๋ค.
๋ค์์ ์ถ๊ฐ์ ์ธ ์๊ตฌ์ฌํญ์ด๋ค.
- ๋ฉ์๋๊ฐ ํ ๊ฐ์ง ์ฑ ์๋ง ๊ฐ์ง๋๋ก ๊ตฌํํ๋ค.
- indent(๋ค์ฌ์ฐ๊ธฐ) ๊น์ด๋ฅผ 1๋จ๊ณ๋ก ์ ์งํ๋ค.
else๋ฅผ ์ฌ์ฉํ์ง ๋ง๋ผ.
์ 3๊ฐ์ง ์์น์ ์ ์ํ๋ ์ด์ ๋ "์์ค์ฝ๋๋ฅผ ์ต๋ํ ๊น๋ํ๊ฒ ๊ตฌํํ ๊ฒ์ ์๊ตฌํ๊ธฐ ์ํจ"์ด๋ค. ์ด๋ณด ํ๋ก๊ทธ๋๋จธ์ ๊ฒฝ์ฐ, ๊ตฌํ์ ์์ด ๋ง๋งํ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค. ์ด๋ฌํ ๋ง๋งํจ์ ํด์ํ๊ธฐ ์ํด ์ 3๊ฐ์ง ์์น์ ์ ์ํ๋ค. (๋ฌผ๋ก ๋ฌด์กฐ๊ฑด ์ ์์น์ ์งํฌ ์๋ ์์ง๋ง, ์ต๋ํ ์งํค๋๋ก ๋ ธ๋ ฅํ๋ฉด ์ข ๋ ๊น๋ํ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ ๊ฒ์ด๋ค.)
๐ง ํ ์คํธ์ ๋ฆฌํฉํ ๋ง์ ํตํ ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ
์๊ตฌ์ฌํญ์ ๋ณต์ก๋๊ฐ ๋์ ์๋ก, ๋น์ฐํ ๊ตฌํ์ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ ์๋ฐ์ ์๋ค. ํ์ง๋ง ๊ฐ๋ฐ์๋ผ๋ฉด ์ด๋ฌํ ๋ณต์ก๋์ ํ์ ๋ถ๋๋ผ๋ฉฐ ์ด์๊ฐ์ผ ํ๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด ๋ณต์ก๋๋ฅผ ๋ฎ์ถ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ ํ์ง ์๊ฒ ๋๊ฐ. ๊ทธ ๋ฐฉ๋ฒ ์ค ํ๋๊ฐ ๋์์๋ ๋ฆฌํฉํ ๋ง์ ํตํด ์์ค์ฝ๋๋ฅผ ๊น๋ํ๊ฒ ๊ตฌํํ๋ ์ฐ์ต์ ํ๋ ๊ฒ์ด๋ค.
๐งญ ์๊ตฌ์ฌํญ์ ์์ ๋จ์๋ก ๋๋๊ธฐ
- ๋ณต์กํ ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํ ์ฒซ ๋ฒ์งธ ์์ ์ "๋ณต์กํ ๋ฌธ์ ๋ฅผ ์์ ๋จ์๋ก ๋๋ ์ข ๋ ์ฌ์ด ๋ฌธ์ ๋ก ๋ณํ"ํ๋ ๊ฒ์ด๋ค. ํ์ง๋ง ์ด๋ฌํ ์์ ์ ์ฝ์ง ์๊ธฐ์ ๋ค์ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํจ์ผ๋ก์จ ์ฐ์ตํ๋ ์๋ฐ์ ์๋ค.
๐งญ ๋ชจ๋ ๋จ๊ณ์ ๋์ ๋ฆฌํฉํ ๋ง
- ์์ค์ฝ๋์ ๋ณต์ก๋๊ฐ ์ฝ๊ฒ ์ฆ๊ฐํ๋ ์ด์ ๋ "ํ๋์ ์๊ตฌ์ฌํญ์ ์๋ฃํ ํ ๋ฆฌํฉํ ๋ง์ ํ์ง ์์ ์ํ์์ ๋ค์ ๋จ๊ณ๋ก ์ด๋"ํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ค์ ๋จ๊ณ๋ก ๋์ด๊ฐ๊ธฐ ์ํ ๋ง์ง๋ง ์์ ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ํ ๋ฆฌํฉํ ๋ง๊น์ง ์๋ฃํ๋ ๊ฒ์ด๋ค.
๐งญ ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ
- ๋น ๋ฌธ์์ด ๋๋ null ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ 0์ ๋ฐํํด์ผ ํ๋ค. (ex.
""=>0,null=>0)
StringCalculator.java
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
return 0;
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
StringCalculatorTest.java
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class StringCalculatorTest {
private StringCalculator cal;
@Before
public void setup() {
cal = new StringCalculator();
}
@Test
public void add_null_๋๋_๋น๋ฌธ์() {
assertEquals(0, cal.add(null));
assertEquals(0, cal.add(""));
}
}
- ๊ตฌํ์ ์์ด ํน๋ณํ ์ ์ ์์ง๋ง, ํ ๊ฐ์ง ๋์ฌ๊ฒจ ๋ณผ ๊ฒ์ ํ ์คํธ ๋ฉ์๋ ์ด๋ฆ์ ํ๊ธ์ ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค. ํ ์คํธ ๋ฉ์๋์ ์ ํํ ์๋ฏธ ์ ๋ฌ์ ์ํด ํ๊ธ๋ก ์์ฑํ ์๋ ์๋ค.
๐งญ ์ซ์ ํ๋๋ฅผ ๋ฌธ์์ด๋ก ์ ๋ ฅํ ๊ฒฝ์ฐ ํด๋น ์ซ์๋ฅผ ๋ฐํํ๋ค. (ex. "1" => 1)
StringCalculator.java
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
return Integer.parseInt(text);
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
StringCalculatorTest.java
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class StringCalculatorTest {
...
@Test
public void add_์ซ์ํ๋() throws Exception {
assertEquals(1, cal.add("1"));
}
}
๐งญ ์ซ์ ๋ ๊ฐ๋ฅผ ์ผํ ๊ตฌ๋ถ์๋ก ์ ๋ ฅํ ๊ฒฝ์ฐ ๋ ์ซ์์ ํฉ์ ๋ฐํํ๋ค. (ex. "1,2" => 3)
StringCalculator.java
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
if (text.contains(",")) {
String[] values = text.split(",");
int sum = 0;
for (String value : values) {
sum += Integer.parseInt(value);
}
return sum;
}
return Integer.parseInt(text);
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
StringCalculatorTest.java
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class StringCalculatorTest {
...
@Test
public void add_์ผํ๊ตฌ๋ถ์() throws Exception {
assertEquals(3, cal.add("1,2"));
}
}
- ์์ ๊ฐ์ ๋ก์ง์ ๊ตฌํํ ๋ ๋ณดํต
if,else if,elseํํ๋ก ๊ตฌํํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด์ง๋ง 3๊ฐ์ง ์์น ์ค ํ๋์ธ "else๋ฅผ ๊ฐ๋ฅํ ์ฌ์ฉํ์ง ๋ง๋ผ"์ ๋ฐ๋ผelse๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌํํ๋ค.
๊ตฌํ ์๋ฃ ํ ํ ์คํธ๊น์ง ์ ์์ ์ผ๋ก ์๋ํ๋ค. ์ด์ ๋ ๋ฆฌํฉํ ๋ง์ ํ ์ฐจ๋ก์ด๋ค.
- ์ฒซ ๋ฒ์งธ๋ก, "์ซ์๊ฐ ํ๋์ธ ๊ฒฝ์ฐ์ ์ผํ ๊ตฌ๋ถ์๋ฅผ ํฌํจํ๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐ๋ก ๋ถ๊ธฐํด์ ์ฒ๋ฆฌํด์ผ ํ๋ค"๋ ์ ์ด๋ค.
String์split()๋ฉ์๋์ ์ซ์ ํ๋๋ฅผ ๊ฐ์ง๋ ๋ฌธ์์ด์ ์ ๋ฌํ์ ๋, ์ซ์ ํ๋๊ฐ ๋ด๊ธดString[]์ ๋ฐํํ๋ค๋ฉด ํด๋น ๋ถ๋ถ์if๋ฌธ์ ์ ๊ฑฐํ ์ ์์ ๊ฒ ๊ฐ๋ค.
SplitTest.java
import static org.junit.Assert.*;
import org.junit.Test;
public class SplitTest {
@Test
public void split() {
assertArrayEquals(new String[] {"1"}, "1".split(","));
assertArrayEquals(new String[] {"1","2"}, "1,2".split(","));
}
}
StringCalculator.java
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
String[] values = text.split(",");
int sum = 0;
for (String value : values) {
sum += Integer.parseInt(value);
}
return sum;
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
SplitTest์ ํ ์คํธ ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋ฏ๋ก, ์์ ๊ฐ์ด ๋ฆฌํฉํ ๋งํ๋ค.if์ ํ๋๋ฅผ ์ ๊ฑฐํด ๋ ๊น๋ํ๊ฒ ๋ง๋ค์๋ค.- ํ์ง๋ง
add()๋ฉ์๋์ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ๊ณ ์์ด ์ซ์์ ํฉ์ ๊ตฌํ๋ ๋ถ๋ถ์ ๋ณ๋์ ๋ฉ์๋๋ก ๋ถ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ๋ค.
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
String[] values = text.split(",");
return sum(values);
}
private int sum(String[] values) {
int sum = 0;
for (String value : values) {
sum += Integer.parseInt(value);
}
return sum;
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
- "๊ฐ ๋ฉ์๋๊ฐ ํ๋์ ์ฑ
์๋ง ์ํํ๊ณ ์๋๊ฐ?" ์ง๋ฌธ์ ๋ํด ์๊ฐํด๋ณด๋ฉด, ์๋ก ๋ถ๋ฆฌํ
sum()๋ฉ์๋๊ฐ ๋ฌธ์์ด ๋ฐฐ์ด์ ์ซ์๋ก ๋ณํํ๋ ์์ ๊ณผ ์ซ์ ๋ฐฐ์ด์ ํฉ์ ๊ตฌํ๋ ์์ ์ ํ๊ณ ์์์ ์ ์ ์๋ค. - ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ฆฌํ๋ค.
public class StringCalculator {
public int add(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
String[] values = text.split(",");
return sum(toInts(values));
}
private int[] toInts(String[] values) {
int[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
numbers[i] = Integer.parseInt(values[i]);
}
return numbers;
}
private int sum(int[] numbers) {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return sum;
}
public static void main(String[] args) {
StringCalculator cal = new StringCalculator();
}
}
-
์์ค ์ฝ๋ ๋ณต์ก๋๊ฐ ๊ฐ์ํ๋ ๊ฒ์ด ์๋๋ผ ์คํ๋ ค ๋ณต์กํด์ง ๋๋์ด๋ค. ์ด๋ ๊ทธ์ ์ฐ์ต์ด๊ธฐ ๋๋ฌธ์ด๋ค.
- ์ค์ ํ๋ก์ ํธ์์ ๋ชจ๋ ์์ค์ฝ๋์ ๋ํด ์ด ๊ฐ์ ๋ฆฌํฉํ ๋ง์ ์งํํ ์ ์๋ค. ํ์ง๋ง ์ด๋ฐ ์์ ์ฝ๋์์ ๊ทน๋จ์ ์ผ๋ก ๋ฆฌํฉํ ๋ง์ ํ๋ ์ฐ์ต์ ํตํด ์ค์ ํ๋ก์ ํธ์์ ๋ฆฌํฉํ ๋งํ ๋ถ๋ถ์ ๋ ์ฝ๊ฒ ์ฐพ์ ์ํํ ์ ์๋ค.
-
๋ฆฌํฉํ ๋งํ ํ ์ฃผ์ ๊น๊ฒ ๋ด์ผํ ๋ถ๋ถ์
private๋ก ๋ถ๋ฆฌํ ๋ฉ์๋๊ฐ ์๋public์ผ๋ก ๊ณต๊ฐํ๊ณ ์๋add()๋ฉ์๋๊ฐ ์ผ๋ง๋ ์ฝ๊ธฐ ์ฝ๊ณ , ์ข์๊ฐ์ด๋ค.- ์ข ๋ ๊ทน๋จ์ ์ผ๋ก ๋ฆฌํฉํ ๋งํ๋ค๋ฉด
text == null || text.isEmpty()๋ํ ๋ค๋ฅธ ๋ฉ์๋๋ก ์ถ์ถํ ์๋ ์๋ค. - ์ด์ ๊ฐ์ด ๊ทน๋จ์ ์ผ๋ก ๋ฆฌํฉํ ๋งํ์ฌ ์์ค์ฝ๋๋ฅผ ์ฝ์ ๋ ํด๋น ๋ฉ์๋๊ฐ ์ด๋ค ์์
์ ์ํํ๋์ง๋ฅผ ์ต๋ํ ์ฝ๊ฒ ํ์
ํ ์ ์์ ๊ฒ์ด๋ค. ์ธ๋ถ ๊ตฌํ์
private๋ฉ์๋๋ก ๋ถ๋ฆฌํ๊ณadd()๋ฉ์๋๊ฐ ๋ฌด์จ ์ผ์ ํ๋ ์ง ๊ธ๋ก ํํํด๋ณด๋ฉด "text ๊ฐ์ด ๋น์ด ์์ผ๋ฉด 0์ ๋ฐํ, ๋น์ด์์ง ์์ผ๋ฉด ๊ตฌ๋ถ์๋ก ๋ถ๋ฆฌ, ์ซ์๋ก ๋ณํํ ๋ค์ ์ด ์ซ์์ ํฉ์ ๊ตฌํ๋ค." ๋ก ํ์ ํ ์ ์๋ค.
- ์ข ๋ ๊ทน๋จ์ ์ผ๋ก ๋ฆฌํฉํ ๋งํ๋ค๋ฉด
"์ธ๋ถ ๊ตฌํ์ ์ง์คํ๋๋ก ํ์ง ์๊ณ ๋ ผ๋ฆฌ์ ์ธ ๋ก์ง์ ์ฝ๊ฒ ํ์ ํ ์ ์๋๋ก ๊ตฌํํ๋ ๊ฒ์ด ์ฝ๊ธฐ ์ข์ ์ฝ๋๋ผ ์๊ฐํ๋ค."
๐งญ ๊ตฌ๋ถ์๋ฅผ ์ผํ ์ด์ธ์ ์ฝ๋ก ์ ์ฌ์ฉํ ์ ์๋ค. (ex. "1,2:3" => 6)
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class StringCalculatorTest {
...
@Test
public void add_์ผํ_๋๋_์ฝ๋ก _๊ตฌ๋ถ์() throws Exception {
assertEquals(6, cal.add("1,2:3"));
}
}
public class StringCalculator {
public int add(String text) {
if (isBlank(text))
return 0;
return sum(toInts(split(text)));
}
private String[] split(String text) {
return text.split(",|:");
}
...
}
- ์๋นํ ์์ ๋ถ๋ถ์ด ์์ ๋์๋ค.
๐งญ "//"์ "\n" ๋ฌธ์ ์ฌ์ด์ Custom ๊ตฌ๋ถ์๋ฅผ ์ง์ ํ ์ ์๋ค. ex."//;\n1;2;3" => 6
@Test
public void add_custom_๊ตฌ๋ถ์() throws Exception {
assertEquals(6, cal.add("//;\n1;2;3"));
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringCalculator {
public int add(String text) {
if (isBlank(text)) {
return 0;
}
return sum(toInts(split(text)));
}
private String[] split(String text) {
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text);
if (m.find()) {
String customDelimeter = m.group(1);
return m.group(2).split(customDelimeter);
}
return text.split(",|:");
}
}
- Custom ๊ตฌ๋ถ์๋ ์ ๊ท ํํ์์ ์ด์ฉํด ๋ฌธ์์ด์ ๋ถ๋ฆฌํ๊ณ ์๋ค. ์ด๋ฅผ ํ์ฉํด ๋ณต์กํ ๋ฌธ์์ด์์ ์ํ๋ ๋ฌธ์์ด์ ์ฐพ๊ฑฐ๋ ํน์ ํ ํจํด์ ์ฐพ์ ์ ์๋ค.
๐งญ ๋ฌธ์์ด ๊ณ์ฐ๊ธฐ์ ์์๋ฅผ ์ ๋ฌํ๋ ๊ฒฝ์ฐ RuntimeException ์์ธ๋ฅผ throw
@Test(expected = RuntimeException.class)
public void add_negative() throws Exception {
cal.add("-1,2,3");
}
- JUnit์์ ๊ธฐ๋ํ๋ ๊ฒฐ๊ณผ ๊ฐ์ด ์์ธ์ธ ๊ฒฝ์ฐ
@Testannotation์expected์์ฑ์ ๊ธฐ๋ํ๋Exceptionํด๋์ค๋ฅผ ์ ๋ฌํ ์ ์๋ค.
...
private int[] toInts(String[] values) {
int[] numbers = new int[values.length];
for (int i = 0; i < values.length; i++) {
numbers[i] = toPositive(values[i]);
}
return numbers;
}
private int toPositive(String value) {
int number = Integer.parseInt(value);
if (number < 0) {
throw new RuntimeException();
}
return number;
}
...
- ์๊ตฌ์ฌํญ์ด ๋ณ๊ฒฝ๋๋ฉด์ ๋ฉ์๋์ ์ด๋ฆ, ๋ณ์ ์ด๋ฆ์ ๋ณ๊ฒฝํ๋ ๊ฒ ๋ํ ๋ฆฌํฉํ ๋ง์ ์ค์ํ ๋ถ๋ถ์ด๋ค.
๐งญ ๊ฐ ๋จ๊ณ์ ๊ฐ๋ฐ๊ณผ์
- ๊ตฌํ => ํ ์คํธ๋ฅผ ํตํด ๊ฒฐ๊ณผ ํ์ธ => ๋ฆฌํฉํ ๋ง
- ๊ฐ๋ฐ์๋ค์ด ์ํํ ํ๋ ๋ถ๋ถ์ด ๋ฆฌํฉํ ๋ง์ด๋ค. ํ์ง๋ง ๊น๋ํ๊ณ ์ฝ๊ธฐ ์ข์ ์ฝ๋๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ๊ฐ๋ฐ์๊ฐ ๊ฐ์ถ์ด์ผ ํ ์ค์ํ ์ญ๋์ด๋ฏ๋ก ๊พธ์คํ ์ฐ์ต์ด ํ์ํ๋ค.
๐งญ 3๊ฐ์ง ์์น
- ๊ฐ๋ฅํ ์์์ ์ธ๊ธํ 3๊ฐ์ง ์์น์ ์งํค๋๋ก ๋ ธ๋ ฅํ์.
- ๋ฆฌํฉํ ๋งํ ๋ถ๋ถ์ ์ฐพ๊ธฐ ํ๋ค ๋, 3๊ฐ์ง ์์น์ ์๊ฐํ๋ฉด์ ์ฐพ๋๋ค๋ฉด ์ฝ๊ฐ์ ํํธ๋ ์ป์ ์ ์์ ๊ฒ์ด๋ค.
๐งญ ๊ทน๋จ์ ์ธ ๋ฆฌํฉํ ๋ง
- ํ๋ก๋์ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ๋๋ผ๋ ๋ฐ๋ก๋ฐ๋ก ํ ์คํธ ์ฝ๋๋ก ๊ฒ์ฆ์ด ๊ฐ๋ฅํ๊ธฐ์ ๊ทน๋จ์ ์ธ ๋ฆฌํฉํ ๋ง์ด ๊ฐ๋ฅํ๋ค.
- ์ค์ ํ๋ก์ ํธ์์๋, ๋ฆฌํฉํ ๋ง์ ํตํด ์์ค์ฝ๋์ ๊ฐ์ ์ ์ํ๋ค๋ฉด, ํ ์คํธ ์ฝ๋๊ฐ ๋ท๋ฐ์นจ๋์ด์ผ ํ๋ค.
"ํ ์คํธ์ ๋ฆฌํฉํ ๋ง์ ๋ถ๋ฆฌํ ์ ์๋ ๋๋ฐ์ ๊ด๊ณ์ด๊ณ ๊ฐ์ด ์ฐ์ตํด์ผ ํ๋ค."
๐ ์ถ์ฒ
์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step : ํ๋์ฉ ๋ฒ๊ฒจ๊ฐ๋ ์ํ๊ป์ง ํ์ต๋ฒ - ๋ฐ์ฌ์ฑ